package agentspeakide.editors;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.reconciler.MonoReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

public class ASLConfiguration extends SourceViewerConfiguration {
	private ASLDoubleClickStrategy doubleClickStrategy;
	private ASLPlanScanner planScanner;
	private ASLDesireScanner scanner;
	private ColorManager colorManager;
	private ASLEditor editor;

	public ASLConfiguration(ColorManager colorManager, ASLEditor editor) {
		this.colorManager = colorManager;
		this.editor = editor;
	}
	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[] {
			IDocument.DEFAULT_CONTENT_TYPE,
			ASLPartitionScanner.ASL_COMMENT,
			ASLPartitionScanner.ASL_COMMENT_LINE,
			ASLPartitionScanner.ASL_PLAN};
	}
	public ITextDoubleClickStrategy getDoubleClickStrategy(
		ISourceViewer sourceViewer,
		String contentType) {
		if (doubleClickStrategy == null)
			doubleClickStrategy = new ASLDoubleClickStrategy();
		return doubleClickStrategy;
	}

	protected ASLDesireScanner getASLDesireScanner() {
		if (scanner == null) {
			scanner = new ASLDesireScanner(colorManager);
			scanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IASLColorConstants.DEFAULT))));
		}
		return scanner;
	}
	protected ASLPlanScanner getASLPlanScanner() {
		if (planScanner == null) {
			planScanner = new ASLPlanScanner(colorManager);
			planScanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IASLColorConstants.PLAN))));
		}
		return planScanner;
	}

	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();
		
		DefaultDamagerRepairer dr =
			new DefaultDamagerRepairer(getASLPlanScanner());
		reconciler.setDamager(dr, ASLPartitionScanner.ASL_PLAN);
		reconciler.setRepairer(dr, ASLPartitionScanner.ASL_PLAN);		

		dr = new DefaultDamagerRepairer(getASLDesireScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

		NonRuleBasedDamagerRepairer ndr =
			new NonRuleBasedDamagerRepairer(
				new TextAttribute(
					colorManager.getColor(IASLColorConstants.ASL_COMMENT)));
		reconciler.setDamager(ndr, ASLPartitionScanner.ASL_COMMENT);
		reconciler.setRepairer(ndr, ASLPartitionScanner.ASL_COMMENT);
		
		ndr =
				new NonRuleBasedDamagerRepairer(
					new TextAttribute(
						colorManager.getColor(IASLColorConstants.ASL_COMMENT_LINE)));
			reconciler.setDamager(ndr, ASLPartitionScanner.ASL_COMMENT_LINE);
			reconciler.setRepairer(ndr, ASLPartitionScanner.ASL_COMMENT_LINE);
			
		return reconciler;
	}
	
	public IReconciler getReconciler(ISourceViewer sourceViewer)
	{
		ASLReconcilingStrategy strategy = new ASLReconcilingStrategy();
		strategy.setEditor(editor);

		MonoReconciler reconciler = new MonoReconciler(strategy,false);

		return reconciler;
	}
	
	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {

		ContentAssistant assistant= new ContentAssistant();
		IContentAssistProcessor processor = new ASLContentAssistProcessor();
		assistant.setContentAssistProcessor(processor, ASLPartitionScanner.ASL_PLAN);

		return assistant;
	}

}